package LeetCode;

import java.util.ArrayList;
import java.util.List;

public class LC_046_Permutations {
    public static void main(String[] args) {

    }

    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        dfs(res, nums, 0);
        return res;
    }

    private void dfs(List<List<Integer>> res, int[] nums, int idx) {
        if (idx == nums.length) {
            List<Integer> list = new ArrayList<>();
            for (int num : nums)
                list.add(num);
            res.add(list);
        } else {
            for (int i = idx; i < nums.length; i++) {
                swap(nums, i, idx);
                dfs(res, nums, idx + 1);
                swap(nums, i, idx);
            }
        }
    }

    private void swap(int[] nums, int m, int n) {
        if (m == n)
            return;
        nums[m] = nums[m] ^ nums[n];
        nums[n] = nums[m] ^ nums[n];
        nums[m] = nums[m] ^ nums[n];
    }
}
